// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: Eggman Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

// Aliases
private alias 58 : TYPE_EGGMAN

// Function declarations
reserve function MCZEggman_Function139
reserve function MCZEggman_Function140
reserve function MCZEggman_Function141

// Static Values

// Tables
public table MCZEggman_table46
	9, 9, 10, 10
end table


function MCZEggman_Function139
	Sin256(object.ypos, object.value2)
	object.ypos <<= 10
	object.ypos += object.value1
	object.value2 += 2
	object.value2 &= 255
end function


function MCZEggman_Function140
	object[-1].angle = object.rotation
	object[+1].angle = object.rotation
	object[-1].frame = object.value18
	object[-1].frame >>= 2
	if object.rotation > 88
		object[-1].frame += 3
		temp0 = object.rotation
		temp0 -= 128
		temp0 &= 511
	else
		temp0 = object.rotation
	end if
	object[+1].frame = object[-1].frame
	object.value18 += object.value19
	object.value18 %= 12
	object[-1].xpos = object.xpos
	object[+1].xpos = object.xpos
	if object.direction == FLIP_NONE
		object[-1].xpos += object.value15
		object[+1].xpos += object.value16
		object[-1].rotation = 512
		object[-1].rotation -= temp0
		object[-1].rotation &= 511
		object[+1].rotation = object[-1].rotation
	else
		object[-1].xpos -= object.value15
		object[+1].xpos -= object.value16
		object[-1].rotation = temp0
		object[+1].rotation = temp0
	end if
	object[-1].ypos = object.ypos
	object[-1].ypos += object.value17
	object[-1].direction = object.direction
	object[+1].ypos = object.ypos
	object[+1].ypos += object.value17
	object[+1].direction = object.direction
end function


function MCZEggman_Function141
	if object.ypos < object.value20
		object.value21++
		object.value21 %= 40
		if object.value21 == 8
			PlaySfx(SfxName[Rumble], 0)
		end if
		temp0 = object.value21
		temp0 &= 3
		if temp0 == 0
			screen.shakeY = 2
		end if
		temp0 = oscillation
		temp0 &= 7
		if temp0 == 0
			temp0 = oscillation
			temp0 &= 15
			if temp0 == 0
				temp0 = 1
			else
				temp0 = 0
			end if
			Rand(temp1, screen.xsize)
			temp2 = object.value21
			temp2 <<= 4
			temp1 += temp2
			temp1 %= screen.xsize
			temp1 -= screen.xcenter
			temp1 <<= 16
			temp1 += object.value6
			temp2 = object.value9
			temp2 += 0x900000
			CreateTempObject(TypeName[Boss Rock], temp0, temp1, temp2)
		end if
	end if
end function


event ObjectMain
	switch object.state
	case 0
		temp0 = object[-1].xpos
		temp0 -= 0x100000
		if object[0].xpos > temp0
			temp0 = object[-1].ixpos
			temp0 -= screen.xcenter
			stage.newXBoundary1 = temp0
			temp0 = object[-1].ixpos
			temp0 += screen.xcenter
			stage.newXBoundary2 = temp0
			temp0 = stage.newYBoundary2
			temp0 -= screen.ysize
			stage.newYBoundary1 = temp0
			object.value6 = object[-1].xpos
			object.value7 = -112
			object.value7 *= screen.xsize
			object.value7 /= 320
			object.value7 <<= 16
			object.value8 = object.value7
			FlipSign(object.value8)
			object.value7 += object[-1].xpos
			object.value8 += object[-1].xpos
			object.value9 = object[-1].ypos
			object.value9 -= 0x1200000
			object.value10 = object[-1].ypos
			object.value10 -= 0x200000
			object.value20 = object[+1].ypos
			object.value20 += 0x280000
			object.xpos = 16
			object.xpos *= screen.xsize
			object.xpos /= 320
			object.xpos <<= 16
			object.xpos += object[-1].xpos
			object.ypos = object.value9
			object.animation = 1
			ResetObjectEntity(26, TypeName[Music Event], 0, 0, 0)
			object[26].priority = PRIORITY_ACTIVE
			CheckCurrentStageFolder("Zone06")
			if checkResult == 1
				object[-1].type = TypeName[Eggman Drill]
				object[+1].type = TypeName[Eggman Drill]
			else
				object[-1].type = TypeName[Eggman Drill 2]
				object[+1].type = TypeName[Eggman Drill 2]
			end if
			object.value14 = 6
			object.value12 = 1
			object.value15 = -0x120000
			object.value16 = 0x160000
			CallFunction(MCZEggman_Function140)
			object.value17 = 0
			object.value3 = 8
			object.priority = PRIORITY_ACTIVE
			object.state = 1
		end if
		break
	case 1
		object.value0++
		if object.value0 == 60
			object.value0 = 0
			object.yvel = 0xC000
			object.value19 = 2
			object.state = 2
		end if
		break
	case 2
		object.ypos += object.yvel
		if object.ypos >= object.value10
			object.ypos = object.value10
			object.value14 = 7
			object.value12 = 0
			object.value19 = 1
			if object[0].xpos > object.xpos
				object.direction = FLIP_X
			end if
			object.state = 4
		end if
		CallFunction(MCZEggman_Function140)
		CallFunction(MCZEggman_Function141)
		break
	case 3
		object.ypos += object.yvel
		if object.ypos <= object.value9
			object.ypos = object.value9
			object.yvel = 0x10000
			if object[0].xpos > object.value6
				object.xpos = object.value7
				object.direction = FLIP_X
			else
				object.xpos = object.value8
				object.direction = FLIP_NONE
			end if
			object.state = 2
		end if
		CallFunction(MCZEggman_Function140)
		CallFunction(MCZEggman_Function141)
		break
	case 4
		object.value0++
		if object.value0 == 100
			object.value0 = 0
			object.value19 = 2
			if object.direction == FLIP_NONE
				object.xvel = -0x20000
				object.state = 6
			else
				object.xvel = 0x20000
				object.state = 7
			end if
		end if
		if object.value0 > 40
			if object.rotation < 128
				object.rotation += 4
			end if
			temp0 = 128
			temp0 -= object.rotation
			temp0 <<= 9
			temp1 = object.rotation
			temp1 <<= 9
			object.value15 = -18
			object.value16 = 22
			object.value15 *= temp0
			object.value16 *= temp0
			object.value17 = 0
			object.value17 *= temp0
			temp2 = -6
			temp3 = 34
			temp2 *= temp1
			temp3 *= temp1
			temp4 = 4
			temp4 *= temp1
			object.value15 += temp2
			object.value16 += temp3
			object.value17 += temp4
		end if
		CallFunction(MCZEggman_Function140)
		break
	case 5
		object.value0++
		if object.value0 == 100
			object.value0 = 0
			object.value19 = 2
			object.yvel = -0xC000
			object.value14 = 6
			object.state = 3
		end if
		if object.value0 > 40
			if object.rotation > 0
				object.rotation -= 4
			end if
			temp0 = 128
			temp0 -= object.rotation
			temp0 <<= 9
			temp1 = object.rotation
			temp1 <<= 9
			object.value15 = -18
			object.value16 = 22
			object.value15 *= temp0
			object.value16 *= temp0
			object.value17 = 0
			object.value17 *= temp0
			temp2 = -6
			temp3 = 34
			temp2 *= temp1
			temp3 *= temp1
			temp4 = 4
			temp4 *= temp1
			object.value15 += temp2
			object.value16 += temp3
			object.value17 += temp4
		end if
		CallFunction(MCZEggman_Function140)
		break
	case 6
		object.xpos += object.xvel
		if object.xpos <= object.value7
			object.xpos = object.value7
			object.value12 = 1
			object.value19 = 1
			object.state = 5
		end if
		CallFunction(MCZEggman_Function140)
		break
	case 7
		object.xpos += object.xvel
		if object.xpos >= object.value8
			object.xpos = object.value8
			object.value12 = 1
			object.value19 = 1
			object.state = 5
		end if
		CallFunction(MCZEggman_Function140)
		break
	case 8
		object.value0++
		if object.value0 == 60
			if object.direction == FLIP_NONE
				object[-1].xvel = -0x10000
			else
				object[-1].xvel = 0x10000
			end if
			object[-1].yvel = -0x20000
			object[-1].state = 1
		end if
		if object.value0 == 120
			if object.direction == FLIP_NONE
				object[+1].xvel = 0x10000
			else
				object[+1].xvel = -0x10000
			end if
			object[+1].yvel = -0x20000
			object[+1].state = 1
		end if
		if object.value0 == 180
			object.value0 = 0
			object.animation = 5
			object.value5 = 0
			object.yvel = 0
			object.state = 9
			ResetObjectEntity(26, TypeName[Music Event], 1, 0, 0)
			object[26].priority = PRIORITY_ACTIVE
			temp0 = tileLayer[0].xsize
			temp0 <<= 7
			stage.newXBoundary2 = temp0
		end if
		break
	case 9
		object.ypos += object.yvel
		object.yvel += 0x1800
		object.value0++
		if object.value0 == 16
			object.yvel = 0
			object.value0 = 0
			object.animationTimer = 0
			object.state++
		end if
		break
	case 10
		if object.value0 < 24
			object.ypos += object.yvel
			object.yvel -= 0x800
			object.value0++
		else
			object.value0 = 0
			object.yvel = 0
			object.value1 = object.ypos
			object.state++
		end if
		break
	case 11
		CallFunction(MCZEggman_Function139)
		if object.value0 < 8
			object.value0++
		else
			object.value0 = 0
			object.animation = 6
			object.value12 = 1
			object.animationTimer = 0
			object.direction = FLIP_X
			object.priority = PRIORITY_ACTIVE
			temp0 = tileLayer[0].xsize
			temp0 <<= 7
			stage.newXBoundary2 = temp0
			object.state++
		end if
		break
	case 12
		object.xpos += 0x40000
		object.value1 -= 0x4000
		CallFunction(MCZEggman_Function139)
		if object.outOfBounds == 1
			ResetObjectEntity(object.entityPos, TypeName[Blank Object], 0, 0, 0)
		end if
		break
	end switch
	if object.value3 != 0
		if object.value4 > 0
			object.value4--
			GetBit(temp0, object.value4, 0)
			if temp0 == 1
				SetPaletteEntry(0, 192, 0xE0E0E0)
			else
				SetPaletteEntry(0, 192, 0)
			end if
		end if
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			if object.value4 < 32
				CheckEqual(object[currentPlayer].animation, ANI_HURT)
				temp0 = checkResult
				CheckEqual(object[currentPlayer].animation, ANI_DYING)
				temp0 |= checkResult
				CheckEqual(object[currentPlayer].animation, ANI_DROWNING)
				temp0 |= checkResult
				if temp0 != 0
					if object.animation != 2
						object.animation = 2
						object.animationTimer = 0
					end if
				end if
				BoxCollisionTest(C_TOUCH, object.entityPos, -24, -24, 24, 24, currentPlayer, object[currentPlayer].value40, object[currentPlayer].value38, object[currentPlayer].value41, object[currentPlayer].value39)
				if checkResult == 1
					CallFunction(PlayerObject_CheckHit)
					if checkResult == 1
						object.value3--
						if object.value3 == 0
							player.score += 1000
							object.value0 = 0
							object.animation = 4
							object.value12 = 0
							object.animationTimer = 0
							object.value5 = 1
							object.value1 = object.ypos
							object.state = 8
							object[-1].interaction = 0
							object[+1].interaction = 0
							SetPaletteEntry(0, 192, 0)
						else
							object.animation = 3
							object.animationTimer = 0
							object.value4 = 64
							PlaySfx(SfxName[Boss Hit], 0)
						end if
					end if
				end if
			end if
		next
	end if
	if object.state != 12
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			temp0 = object[currentPlayer].collisionRight
			temp0 <<= 16
			temp0 += object[currentPlayer].xpos
			temp1 = stage.curXBoundary2
			temp1 <<= 16
			if temp0 > temp1
				object[currentPlayer].xvel = 0
				object[currentPlayer].speed = 0
				object[currentPlayer].xpos = temp1
				temp0 = object[currentPlayer].collisionRight
				temp0 <<= 16
				object[currentPlayer].xpos -= temp0
			end if
		next
	end if
	if object.value5 == 1
		temp0 = oscillation
		temp0 &= 7
		if temp0 == 0
			Rand(temp0, 48)
			temp0 -= 24
			temp0 <<= 16
			temp0 += object.xpos
			Rand(temp1, 48)
			temp1 -= 24
			temp1 <<= 16
			temp1 += object.ypos
			CreateTempObject(TypeName[Explosion], 0, temp0, temp1)
			object[tempObjectPos].drawOrder = 5
			PlaySfx(SfxName[Explosion], 0)
		end if
	end if
end event


event ObjectDraw
	DrawSpriteFX(6, FX_FLIP, object.xpos, object.ypos)
	if object.value14 == 7
		DrawSpriteFX(7, FX_FLIP, object.xpos, object.ypos)
	end if
	switch object.animation
	case 1
		temp0 = object.animationTimer
		temp0 >>= 3
		if saveRAM[495] == 0
			object.animationTimer++
			object.animationTimer %= 15
		endif
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break
	case 2
		temp0 = object.animationTimer
		temp0 &= 15
		temp0 >>= 3
		temp0 += 2
		object.animationTimer++
		if object.animationTimer == 50
			object.animationTimer = 0
			if object[0].animation != ANI_DYING
				object.animation = 1
			end if
		end if
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break
	case 3
		temp0 = object.animationTimer
		temp0 &= 1
		temp0 += 2
		object.animationTimer++
		if object.animationTimer == 50
			object.animationTimer = 0
			object.animation = 1
		end if
		DrawSpriteFX(4, FX_FLIP, object.xpos, object.ypos)
		break
	case 4
		temp0 = object.animationTimer
		temp0 >>= 5
		temp0 += 4
		temp0 %= 5
		object.animationTimer++
		object.animationTimer &= 63
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break
	case 5
		DrawSpriteFX(5, FX_FLIP, object.xpos, object.ypos)
		break
	case 6
		temp0 = object.animationTimer
		temp0 >>= 3
		object.animationTimer++
		object.animationTimer &= 15
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		temp0 = object.animationTimer
		temp0 >>= 2
		temp0 &= 1
		if temp0 == 1
			DrawSpriteFX(11, FX_FLIP, object.xpos, object.ypos)
		end if
		break
	end switch
	switch object.value12
	case 0
		DrawSpriteFX(8, FX_FLIP, object.xpos, object.ypos)
		break
	case 1
		GetTableValue(temp0, object.value13, MCZEggman_table46)
		object.value13++
		object.value13 &= 3
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break
	end switch
end event


event ObjectStartup
	CheckCurrentStageFolder("Zone06")
	if checkResult == 1
			if saveRAM[495] == 0
				LoadSpriteSheet("MCZ/Objects.gif")
			else
				LoadSpriteSheet("MCZ/ObjectsE.gif")
			endif
		SpriteFrame(-26, -24, 44, 16, 34, 48)
		SpriteFrame(-26, -24, 44, 16, 79, 48)
		SpriteFrame(-26, -24, 44, 16, 34, 65)
		SpriteFrame(-26, -24, 44, 16, 79, 65)
		SpriteFrame(-26, -24, 44, 16, 34, 82)
		SpriteFrame(-26, -24, 44, 16, 79, 82)
		SpriteFrame(-38, -37, 70, 58, 74, 197)
		SpriteFrame(-34, 0, 17, 16, 5, 234)
		SpriteFrame(-8, 18, 24, 8, 207, 152)
		SpriteFrame(-8, 18, 24, 16, 190, 165)
		SpriteFrame(-8, 18, 24, 32, 176, 132)
		SpriteFrame(3, -28, 11, 8, 76, 132)
		if options.vsMode == 1
			foreach (TypeName[Eggman], arrayPos0, ALL_ENTITIES)
				object[arrayPos0].type = TypeName[Blank Object]
			next
		end if
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-26, -24, 44, 16, 1, 5)
		SpriteFrame(-26, -24, 44, 16, 62, 5)
		SpriteFrame(-26, -24, 44, 16, 1, 26)
		SpriteFrame(-26, -24, 44, 16, 62, 26)
		SpriteFrame(-26, -24, 44, 16, 1, 47)
		SpriteFrame(-26, -24, 44, 16, 62, 47)
		SpriteFrame(-38, -37, 70, 58, 182, 197)
		SpriteFrame(-34, 0, 17, 16, 182, 180)
		SpriteFrame(-8, 18, 24, 8, 388, 227)
		SpriteFrame(-8, 18, 24, 16, 388, 210)
		SpriteFrame(-8, 18, 24, 32, 388, 177)
		SpriteFrame(3, -28, 11, 8, 45, 88)
	end if
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
